package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Pdfl1;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class Pdfl1Form extends BasePage implements Serializable {
    private GenericManager<Pdfl1, Long> pdfl1Manager;
    private Pdfl1 pdfl1 = new Pdfl1();
    private Long id;

    public void setPdfl1Manager(GenericManager<Pdfl1, Long> pdfl1Manager) {
        this.pdfl1Manager = pdfl1Manager;
    }

    public Pdfl1 getPdfl1() {
        return pdfl1;
    }

    public void setPdfl1(Pdfl1 pdfl1) {
        this.pdfl1 = pdfl1;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        pdfl1Manager.remove(pdfl1.getId());
        addMessage("pdfl1.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            pdfl1 = pdfl1Manager.get(id);
        } else {
            pdfl1 = new Pdfl1();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (pdfl1.getId() == null || pdfl1.getId() == 0);
        
        pdfl1.setUsername(getRequest().getRemoteUser());
        pdfl1.setOptdate(new Date());
        pdfl1.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        pdfl1Manager.save(pdfl1);

        String key = (isNew) ? "pdfl1.added" : "pdfl1.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	  this.id=this.pdfl1.getId();
              return edit();
        }
    }
} 